package com.aabte.commons.errorcode;

import com.aabte.commons.errorcode.exception.BaseException;

import java.util.Collection;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/4/30
 */
public interface Assert {

  /**
   * 创建异常
   *
   * @param args
   * @return BaseException
   */
  BaseException newException(Object... args);

  /**
   * 创建异常
   *
   * @param message
   * @param args
   * @return BaseException
   */
  BaseException newException(String message, Object... args);

  /**
   * 创建异常
   *
   * @param t
   * @param args
   * @return BaseException
   */
  BaseException newException(Throwable t, Object... args);

  /**
   * 创建异常
   *
   * @param message
   * @param t
   * @param args
   * @return BaseException
   */
  BaseException newException(Throwable t, String message, Object... args);

  default void checkNotNull(Object obj) {
    if (obj == null) {
      throw newException((Object[]) null);
    }
  }

  default void checkNotNull(Object obj, String message) {
    if (obj == null) {
      throw newException(message, (Object[]) null);
    }
  }

  default void checkNotNull(Object obj, Object... args) {
    if (obj == null) {
      throw newException(args);
    }
  }

  default void checkNotNull(Object obj, String message, Object... args) {
    if (obj == null) {
      throw newException(message, args);
    }
  }

  default void checkNotEmpty(Collection<?> obj, Object... args) {
    if (obj == null || obj.size() == 0) {
      throw newException(args);
    }
  }

  default void checkNotEmpty(Collection<?> obj, String message, Object... args) {
    if (obj == null || obj.size() == 0) {
      throw newException(message, args);
    }
  }

  default void checkNotEmpty(String obj, Object... args) {
    if (obj == null || obj.length() == 0) {
      throw newException(args);
    }
  }

  default void checkNotEmpty(String obj, String message, Object... args) {
    if (obj == null || obj.length() == 0) {
      throw newException(message, args);
    }
  }

  default void checkNotBlank(String obj, Object... args) {
    if (obj == null || obj.trim().length() == 0) {
      throw newException(args);
    }
  }

  default void checkNotBlank(String obj, String message, Object... args) {
    if (obj == null || obj.trim().length() == 0) {
      throw newException(message, args);
    }
  }

  default void checkArgument(boolean expression, Object... args) {
    if (!expression) {
      throw newException(args);
    }
  }

  default void checkArgument(boolean expression, String message, Object... args) {
    if (!expression) {
      throw newException(message, args);
    }
  }
}
